<%-- 
    Document   : clients
    Created on : Mar 4, 2014, 10:36:09 PM
    Author     : Theodoro
--%>
<%@page contentType="text/html" pageEncoding="UTF-8" errorPage="error.jsp" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<sql:setDataSource user="root" password="" driver="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost/bonix"/>

<c:if test="${sessionScope.username == NULL}">
    <c:redirect url="http://webtek.org/Bonix/php/public/index.php"/>
</c:if>
<c:set var="dt" value="<%=new java.util.Date()%>"/>
<fmt:formatDate scope="session" var="now" value="${dt}" pattern="yyyy-MM-dd" />
<c:set var="selectedDate" value="${sessionScope.now}"/>

<sql:query var="profile">
    SELECT username, first_name, last_name, contact
    FROM profile 
</sql:query>

<sql:query var="messages">
    SELECT * 
    FROM messages
    WHERE receiver = '${sessionScope.username}' and status = 'unread'
</sql:query>

<sql:query var="potentialClients">
    SELECT distinct sender
    FROM messages
    WHERE receiver = '${sessionScope.username}'
</sql:query>  

<sql:query var="customers" >
    SELECT cust
    FROM contracts
    WHERE servp ='${sessionScope.username}'
    ORDER by date

</sql:query>
<!DOCTYPE html>
<html>
    <c:set var="startFromRecNo" value="0"/>
    <c:set var="newMessages" value="0"/>
    <c:set var="sorting" value="Ongoing"/>    
    <c:if test="${param.start != null}">
        <c:set var="startFromRecNo" value="${param.start}"/>
    </c:if>


    <c:if test="${sessionScope.messageSort != null}" >
        <c:set var="sorting" value="${sessionScope.messageSort}"/>
    </c:if>

    <c:if test="${sorting == 'Finished'}">
        <sql:query var="contracts" maxRows="8" startRow="${startFromRecNo}">
            SELECT *
            FROM contracts
            WHERE status = 'finished'
            ORDER by date DESC
        </sql:query>
    </c:if>
    <c:if test="${sorting == 'Ongoing'}">
        <sql:query var="contracts" maxRows="8" startRow="${startFromRecNo}">
            SELECT *
            FROM contracts
            WHERE status = 'on going'
            ORDER by date DESC
        </sql:query>
    </c:if>

    <c:set scope="request" var="title" value="Service Provider"/>
    <c:set scope="session" var="customer" />
    <jsp:include page="head.jsp"/>
    <body>
        <jsp:include page="banner.jsp"/>
        <article id="content">
            <!-- Header(title of page) should be dynamic -->
            <section class="container graph">
                <div class="tab">
                    <span class="title">CLIENTS:${sorting}</span>
                </div>

                <div class="inner" id="bonixTable">
                    <c:if test="${startFromRecNo > 0}">
                        <c:url var="prevURL" value="clients.jsp">
                            <c:param name="start" value="${startFromRecNo - 8}"></c:param>
                        </c:url>
                        <a href="${prevURL}" class="btn btn-primary" role="button">Previous</a>
                    </c:if>

                    <c:if test="${contracts.limitedByMaxRows}">
                        <c:url var="nextURL" value="clients.jsp">
                            <c:param name="start" value="${startFromRecNo + 8}"></c:param>
                        </c:url>
                        <a href="${prevURL}" class="btn btn-primary" role="button">Next</a>
                    </c:if>
                    <br><br>
                    <table class="table table-hover table-condensed">
                        <thead>  
                            <tr>  
                                <th>Customer</th>
                                <th>Contact</th>
                                <th>Job Description</th>  
                                <th>Date</th>  
                            </tr>  
                        </thead>
                        <tbody>
                            <c:forEach var="contract" items="${contracts.rows}">
                                <tr>
                                    <c:url var="composeMessageURL" value="viewConversation.jsp">
                                        <c:param name="sender" value="${contract.cust}"/>
                                    </c:url>
                                    <td><a href="${composeMessageURL}">${contract.cust}</a></td>
                                        <c:forEach var="account" items="${profile.rows}">
                                            <c:if test="${account.username == contract.cust}">
                                            <td>${account.contact}</td>
                                        </c:if>       
                                    </c:forEach>
                                    <td>${contract.description}</td>
                                    <td>${contract.date}</td>                                               
                                </tr>
                            </c:forEach>
                        </tbody>
                    </table>
                    <form action="sortCustomers.jsp" method="POST">
                        <select name="messageStatus" style="height:45px">
                            <option>Ongoing</option>
                            <option>Finished</option>
                        </select>
                        <input type="submit" value="go" class="btn btn-primary">
                    </form>

                </div>
            </section>

            <section class="container others">
                <div class="tab">
                    <span class="title">Create Transaction</span>
                </div>
                <div class="inner">
                    <div id="addTask">
                        <form action="savedata.jsp?source=viewConversation" method="POST">
                            <label>CUSTOMER:</label><br>
                            <select name="cust" style="width:180px;height:30px">
                                <option style="width:180px;height:30px">${sessionScope.sender}</option>
                                <c:forEach var="customer" items="${customers.rows}">
                                    <option style="width:180px;height:30px">${customer.sender}</option>
                                </c:forEach>
                            </select><br>
                            <label>JOB DESCRIPTION:</label><br>
                            <input type="text" name="description"><br>
                            <label>DATE:</label><br>
                            <input type="date" name="scheduledDate" ><br>
                            <input type="submit" value="Create" class="btn btn-primary">
                        </form>
                    </div>

                </div>
            </section>
        </article>
    </body>
</html>